package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.UserConstant;
import com.tbit.main.pojo.*;
import com.tbit.main.service.AccountCheckingService;
import com.tbit.main.service.AccountService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 对账接口
 *
 * @author LMY
 * @create 2022-03-30 14:35
 */
@RestController
@RequestMapping("/checking")
public class AccountCheckingController {

    @Autowired
    private AccountCheckingService accountCheckingService;

    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountService accountService;

    /**
     * 品牌，区域总数据
     *
     * @param token
     * @param accountIds
     * @param time
     * @return
     */
    @RequestMapping("/totalData")
    public JsonResult totalData(String token, @RequestParam(value = "accountIds", required = false) List<Integer> accountIds, String time) {

        AccountUser accountuser = tokenService.getAccount(token);
        String startTime = null;
        String endTime = null;
        if (time != null) {
            startTime = DateTimeUtil.getMothStartTime(time);
            endTime = DateTimeUtil.getMothEndTime(time);
        }
        List<Account> accountList = new LinkedList<>();
        /**区域*/
        if (accountIds != null && !accountIds.isEmpty()) {
            List<BrandDataDaySta> brandDataDaySta = accountCheckingService.totalDataAccountId(accountIds, startTime, endTime);
            return JsonResult.succ(brandDataDaySta);
        } else {
            /**品牌*/
            Integer accountId;
            // 品牌
            if (accountService.getCountHolderAgent(accountuser.getAccountUserId()) > 0) {
                accountList = accountService.getHolderAgent(accountuser.getAccountUserId());
            } else if ((accountuser.getAccountUserType().equals(UserConstant.ACCOUNT_BRAND) ||
                    accountuser.getRole().getRoleLevel().equals(UserConstant.ACCOUNT_BRAND))
                    && !accountuser.getAccountUserType().equals(UserConstant.ACCOUNT_FRANCHISEE)
                    && !accountuser.getAccountUserType().equals(UserConstant.ACCOUNT_ADOPT)) {
                accountId = accountuser.getAccountId();
                BrandDataDaySta brandDataDaySta = accountCheckingService.totalDataBrandId(accountId, startTime, endTime);
                return JsonResult.succ(brandDataDaySta);
            }// 加盟商
            else if ((accountuser.getAccountUserType().equals(UserConstant.ACCOUNT_AREA) ||
                    accountuser.getRole().getRoleLevel().equals(UserConstant.ACCOUNT_AREA))
                    && !accountuser.getAccountUserType().equals(UserConstant.ACCOUNT_FRANCHISEE)) {
                /** 加载加盟商下面的所有运营区域 */
                accountList = accountService.getByParentId(accountuser.getAccountId());
            }
            //领养人
            else if (accountuser.getAccountUserType().equals(UserConstant.ACCOUNT_ADOPT)) {
            }
            // 区域运营商、运维管理员、运维人员
            else {
                List<Integer> accountIdS = new ArrayList<Integer>();
                accountIdS.add(accountuser.getAccountId());
                accountList = accountService.getByAccountIds(accountIdS);
            }

            accountIds = accountList.stream().map(Account::getAccountId).collect(Collectors.toList());

            List<BrandDataDaySta> brandDataDaySta = accountCheckingService.totalDataAccountId(accountIds, startTime, endTime);
            return JsonResult.succ(brandDataDaySta);

        }
    }


    /**
     * 品牌数据日，月统计。
     *
     * @param token
     * @param time
     * @param type
     * @return
     */
    @RequestMapping("/brand")
    public JsonResult brand(String token, String time, Integer type) {
        String startTime = null;
        String endTime = null;
        if (time != null) {
            startTime = DateTimeUtil.getMothStartTime(time);
            endTime = DateTimeUtil.getMothEndTime(time);
        }

        List<BrandDataDaySta> brandDataDayStaList;

        AccountUser accountUser = tokenService.getAccount(token);

        Integer accountId;
        if (accountUser.getAccountUserType().equals(UserConstant.ACCOUNT_BRAND) || accountUser.getRole().getRoleLevel().equals(UserConstant.ACCOUNT_BRAND)) {
            accountId = accountUser.getAccountId();
        }
        //非品牌账号
        else {
            accountId = accountService.getByAccountAndType(accountUser.getAccountId(), 1).getAccountId();
        }

        /**日*/
        if (type.equals(1)) {
            brandDataDayStaList = accountCheckingService.brandIdDay(accountId, startTime, endTime);
        } else {
            /**月*/
            brandDataDayStaList = accountCheckingService.brandIdMonth(accountId, startTime, endTime);
        }

        if (brandDataDayStaList == null) {
            return JsonResult.succ(new LinkedList<BrandDataDaySta>());
        }

        return JsonResult.succ(brandDataDayStaList);
    }


    /***
     * 区域数据日，月统计。
     * @param token
     * @param accountIds
     * @param time
     * @param type
     * @return
     */
    @RequestMapping("/account")
    public JsonResult account(String token, @RequestParam(value = "accountIds", required = true) List<Integer> accountIds, String time, Integer type) {

        String startTime = null;
        String endTime = null;
        if (time != null) {
            startTime = DateTimeUtil.getMothStartTime(time);
            endTime = DateTimeUtil.getMothEndTime(time);
        }

        Map<String, List<AccountDataDayStaReturn>> map;
        /**日*/
        if (type.equals(1)) {
            map = accountCheckingService.accountIdDay(accountIds, startTime, endTime);
        } else {
            /**月*/
            map = accountCheckingService.accountIdMonth(accountIds, DateTimeUtil.getYYMM_(time), DateTimeUtil.getYYMM_(time));
        }

        if (map == null) {
            return JsonResult.succ(new HashMap<Integer, List<AccountDataDaySta>>());
        }

        return JsonResult.succ(map);
    }

}
